package com.gopay.bis.service.bank.cbrmb.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.gopay.bis.manager.bank.cbrmb.MasaPayManager;
import com.gopay.bis.service.bank.cbrmb.MasaPayService;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayBankDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayBankDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayEnrolmentCheckDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayEnrolmentCheckDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayOrderDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayOrderDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayOrderNotifyDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayPayerAuthValidateReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayPayerAuthValidateResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryOrderDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryOrderDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryRefundOrderDTOReq;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayQueryRefundOrderDTOResp;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayRefundOrderNotifyDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayUploadExpressListDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.MasaPayUploadExpressListResDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.RefundReqDTO;
import com.gopay.common.domain.cbrmb.internationalcard.masapay.RefundResDTO;
import com.gopay.common.exception.CPSException;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.cps.cbrmb.CbRmbMasaPayNotifyRemoteService;

/**
 * 
 * @ClassName: MasaPayRemoteServiceImpl
 * @Description: TODO
 * @author gd.cui
 * @date 2017年10月25日 下午2:08:44
 */
@Service("masaPayService")
public class MasaPayServiceImpl implements MasaPayService {

	private static final Logger logger = LoggerFactory.getLogger(MasaPayServiceImpl.class);

	@Resource(name = "masaPayManager")
	private MasaPayManager masaPayManager;

	@Resource(name = "cbRmbMasaPayNotifyService")
	private CbRmbMasaPayNotifyRemoteService cbRmbMasaPayNotifyService;

	@Override
	public MasaPayOrderDTOResp payOrder(MasaPayOrderDTOReq req) throws GopayException {
		return masaPayManager.payOrder(req);
	}

	@Override
	public RefundResDTO refundOrder(RefundReqDTO refundReqDTO) throws GopayException {
		return masaPayManager.refundOrder(refundReqDTO);
	}

	@Override
	public MasaPayQueryOrderDTOResp queryOrder(MasaPayQueryOrderDTOReq req) throws GopayException {
		return masaPayManager.queryOrder(req);
	}

	@Override
	public String orderNoitfy(MasaPayOrderNotifyDTO orderNotify) {
		boolean notify = masaPayManager.signOrderNotify(orderNotify);
		if (notify) {
			logger.error("cps orderNoitfy doing");
			String result = cbRmbMasaPayNotifyService.orderNoitfy(orderNotify);
			if ("success".equals(result)) {
				logger.error("cps orderNoitfy success");
				return "OK";
			}
		}
		return null;
	}

	@Override
	public String refundOrderNoitfy(MasaPayRefundOrderNotifyDTO refundOrderNotify) {
		boolean notify = masaPayManager.signRefundOrderNotify(refundOrderNotify);
		if (notify) {
			logger.error("cps notify doing");
			try {
				String result = cbRmbMasaPayNotifyService.refundOrderNoitfy(refundOrderNotify);
				if ("success".equals(result)) {
					logger.error("cps notify success");
					return "OK";
				}
			} catch (CPSException e) {
				logger.error("cps notify fail:" + e);
			}
		}
		return null;
	}

	@Override
	public MasaPayQueryRefundOrderDTOResp queryRefundOrder(MasaPayQueryRefundOrderDTOReq req) throws GopayException {
		return masaPayManager.queryRefundOrder(req);
	}

	@Override
	public MasaPayUploadExpressListResDTO uploadExpress(MasaPayUploadExpressListDTO req) throws GopayException {
		return masaPayManager.uploadExpress(req);
	}
	
	@Override
	public MasaPayEnrolmentCheckDTOResp enrolmentCheck(MasaPayEnrolmentCheckDTOReq req) throws GopayException {
		return masaPayManager.enrolmentCheck(req);
	}

	@Override
	public MasaPayBankDTOResp authenticationPayer(MasaPayBankDTOReq req) throws GopayException {
		return masaPayManager.authenticationPayer(req);
	}

	@Override
	public MasaPayPayerAuthValidateResp payerAuthValidate(MasaPayPayerAuthValidateReq req) throws GopayException {
		return masaPayManager.payerAuthValidate(req);
	}

}
